// miniprogram/pages/order/order.js
const db = wx.cloud.database()
const app = getApp()
import Dialog from '../../vant/dialog/dialog';
import Toast from '../../vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showBackArrow: true,
    title: '确定订单',
    priceSum: 0,
    addressInfo: null,
    addressInfoText: null,
    chosenGoodsList: [],
    submitLoading: false,
    // 是否付款
    isPay: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //获取页面信息
    if(options.isPay) {
      //从我的页面跳转过来的
      let addressInfo = JSON.parse(options.addressInfo)
      this.setData({
        priceSum: parseFloat(options.priceSum),
        chosenGoodsList: JSON.parse(options.listData),
        isPay: options.isPay,
        addressInfo: addressInfo,
        addressInfoText: `${addressInfo.cityName} ${addressInfo.countyName} ${addressInfo.detailInfo} `
      })
    } else {
      this.setData({
        priceSum: parseFloat(options.priceSum),
        chosenGoodsList: JSON.parse(options.listData)
      })
    }
  },
  tapClickBack() {
    // 返回前一页面
    wx.navigateBack({
      delta: 1
    })
  },
  chooseAddress() {
    wx.chooseAddress({
      success: (res) => {
        this.setData({
          addressInfo: res,
          addressInfoText: `${res.cityName} ${res.countyName} ${res.detailInfo} `
        })
      },
      fail: console.err
    })
  },
  jumpToDetail(e) {
    let index = e.target.dataset.index
    let goodId = this.data.chosenGoodsList[index].goodDetail.goodId
    wx.navigateTo({
      url: '/pages/detail/detail?goodId=' + goodId
    })
  },
  onClickSubmitSure() {
    if(!this.data.addressInfo) {
      Toast.fail('请选择地址！')
      return false
    }
    this.setData({
      submitLoading: true
    })
    // 选择地址后的确定订单
    wx.cloud.callFunction({
      name: 'addList',
      data: {
        listName: 'order_list',
        form: {
          _openid: app.globalData.openid,
          orderId: app.guid(),
          status: 0,
          goodList: this.data.chosenGoodsList,
          addressInfo: this.data.addressInfo,
          enable: 1
        }
      },
      success: res => {
        this.setData({
          submitLoading: false
        })
        Toast.success('提交成功!')
        this.removeCartData()
      },
      fail: err => {
        console.err('失败')
      }
    })
  },
  removeCartData() {
    // 获取选中的cartId数组
    let cartIdArr = []
    let list = this.data.chosenGoodsList
    for(let i = 0, len = list.length; i < len; i ++) {
      cartIdArr.push(list[i].cartId)
    }
    // 确认订单后  需要删除购物车数据
    const _ = db.command
    wx.cloud.callFunction({
      name: 'updateList',
      data: {
        listName: 'cart_list',
        queryJson: {
          cartId: _.in(cartIdArr)
        },
        newData: {
          "enable": 0
        }
      },
      success: res => {
        wx.switchTab({
          url: '/pages/mine/mine'
        })
      },
      fail: err => {
        Toast.fail('删除cart_list失败')
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})